
require('../../assets/css/index.js');//引入的是公共css和复用css
const dom = require('../../utils/dom.js');  //引入节点操作js
require('../../lib/captcha/regjigsawmin.js');
let https = require('../../utils/https.js');
require('./LogIn.less');//自己

//验证用户有没有这个账号
dom.getE('#Tel').addEventListener('blur', () => {
  https.post('/api/user/isExist', {
    account: dom.getE('#Tel').value,
  }, res =>
    res.errno === 10002 ? alert(dom.getE('#Tel').value + '用户名已注册，请输入密码') : alert(dom.getE('#Tel').value + '不可以登录，没有这个账号                       '))
})
//用户提交后保存令牌

function BTN() {
  https.post('/api/user/login', {
    account: dom.getE('#Tel').value,
    password: dom.getE('#passwordx').value
  }, res => {
    //保存token在本地
    if (dom.getE('#passwordx').value === '' || dom.getE('#Tel').value === '') {
      alert('密码账号都不能为空')
      history.go(0)
    } else if (res.errno === 20002) {
      alert('密码错误')
      history.go(0)
    } else if (res.errno === 0) {
      alert('登录成功，即将跳转')
      localStorage.setItem("tk", res.data.token)
    }
  })
}



//点击显示密码
let yanjing1
dom.getE('.icon-mimaxianshi').addEventListener('click', () => {
  if (yanjing1) {
    dom.getE('#passwordx').type = 'text'
    yanjing1 = false
  } else {
    dom.getE('#passwordx').type = 'password'
    yanjing1 = true
  }
})

dom.getE('.btn').addEventListener('click', () => {
  dom.getE('.Imagverification').classList.remove('off')
  jigsaw.init({
    el: dom.getE('#captcha'),
    onSuccess: function () {
      alert('验证通过')
      dom.getE('.Imagverification').classList.add('off')
      BTN()
    }
  })
})
